﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SableCC15.CodeGeneration
{
    /// <summary>
    /// Utility class for code generation
    /// </summary>
    public static class Utility
    {
        static StreamWriter oStreamWriter;
        static string _path;

        public static void ResetFile()
        {
            string path = Path.GetFullPath(Assembly.GetExecutingAssembly().Location);
            path = Directory.GetParent(path).ToString();
            path = Directory.GetParent(path).ToString();
            path = Directory.GetParent(path).ToString();
            path = Directory.GetParent(path).ToString();
            _path = path;
            _path += @"\MarzFramework\Target.cs";
            //string path = @"C:\Users\brugeren\Documents\MARZScript\SableCC15\MarzFramework\Target.cs";//Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory) + "Target.cs";
            if (File.Exists(_path))
            {
                File.WriteAllText(_path, String.Empty);
            }

        }
        /// <summary>
        /// The Main emit method.
        /// </summary>
        /// <param name="str"></param>
        //[Obsolete("NULLREFERENCE EXCEPTION, You cannot execute this method yet!", true)]
        public static void Emit(string str)
        {
            //string path = @"C:\Users\brugeren\Documents\MARZScript\SableCC15\MarzFramework\Target.cs";//Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory) + "Target.cs";
            if (File.Exists(_path))
            {
                File.AppendAllText(_path, str, Encoding.Default);
            }
        }

        /// <summary>
        /// Emmits the comments!
        /// </summary>
        /// <param name="str"></param>
        public static void EmitComment(string str)
        {
            Emit("//" + str + "\r\n");
        }

        /// <summary>
        /// For skipping and emitting nothing.
        /// </summary>
        /// <param name="num"></param>
        public static void Skip(int num)
        {
            for (int i = 0; i < num; i++)
            {
                Emit("");
            }
        }

        /// <summary>
        /// Extracts the YCoords from the input
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string YCoords(string str)
        {
            str = str.Replace(" ", string.Empty);
            string pattern = @"\(\d+,";
            string y = "";
            if (Regex.Match(str, @"\d+\.\d+").Success)
                y = Regex.Replace(str, @"\(\d+\.\d+,", string.Empty).Replace(")", string.Empty);
            else
                y = Regex.Replace(str, pattern, string.Empty).Replace(")", string.Empty);
            if (str.Contains("+") && !Regex.Match(y, @"\d+\+\d+").Success)
                y = y.Replace("+", string.Empty);
            else if (str.Contains("-") && !Regex.Match(y, @"\d+\-\d+").Success)
                y = y.Replace("-", string.Empty);
            else if (str.Contains("*") && !Regex.Match(y, @"\d+\*\d+").Success)
                y = y.Replace("*", string.Empty);
            else if (str.Contains("/") && !Regex.Match(y, @"\d+\/\d+").Success)
                y = y.Replace("/", string.Empty);
            return y;
        }

        /// <summary>
        /// Extracts the XCoords from the input
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string XCoords(string str)
        {
            str = str.Replace(" ", string.Empty);
            string pattern = @",\d+\)";
            string x = "";
            if (Regex.Match(str, @"\d+\.\d+").Success)
                x = Regex.Replace(str, @",\d+\.\d+\)", string.Empty).Replace("(", string.Empty);
            else
                x = Regex.Replace(str, pattern, string.Empty).Replace("(", string.Empty);

            if (str.Contains("+") && !Regex.Match(x, @"\d+\+\d+").Success)
                x = x.Replace("+", string.Empty);
            else if (str.Contains("-") && !Regex.Match(x, @"\d+\-\d+").Success)
                x = x.Replace("-", string.Empty);
            else if (str.Contains("*") && !Regex.Match(x, @"\d+\*\d+").Success)
                x = x.Replace("*", string.Empty);
            else if (str.Contains("/") && !Regex.Match(x, @"\d+\/\d+").Success)
                x = x.Replace("/", string.Empty);
            return x;
        }
    }
}
